package org.robotjuice.turtleflip;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Main activity
 * @author matias
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */
public class TurtleFlip extends Activity
{
	private TextView bubbleText;
	private ImageView turtleImage;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); // Hide title bar
		setContentView(R.layout.main);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Always keep portrait mode
		
		// Start TurtleService
		//startService(new Intent(this, TurtleService.class));
		
		turtleImage = (ImageView) findViewById(R.id.logo);
		AlphaAnimation alpha = new AlphaAnimation((float)0.5, (float)1.0);
		alpha.setDuration(1000);
		alpha.setFillAfter(true);
		turtleImage.startAnimation(alpha);
		
		// TODO: Fix this not behaving correctly
		turtleImage.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				if(TurtleService.getContext() != null)
					stopTurtleService();
				else
					startTurtleService();
			}
		});

		// Animate speech bubble
		LinearLayout speechBubble = (LinearLayout) findViewById(R.id.speech_bubble);
		AlphaAnimation bubbleAlpha = new AlphaAnimation((float)0, (float)1.0);
		bubbleAlpha.setDuration(1000);
		bubbleAlpha.setFillAfter(true);
		speechBubble.startAnimation(bubbleAlpha);
		
		bubbleText = (TextView) findViewById(R.id.bubble_text);
	}
	
	@Override
	public void onStart()
	{
		super.onStart();
		
		if(TurtleService.getContext() != null)
			bubbleText.setText("Press me to disable Turtle Flip");
		else
			bubbleText.setText("Press me to enable Turtle Flip");
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
	}
	
	@Override
	public void onDestroy() 
	{
		super.onDestroy();
		//stopService(new Intent(this, TurtleService.class));
	}
	
	private void startTurtleService()
	{
		startService(new Intent(TurtleFlip.this, TurtleService.class));
		bubbleText.setText("Press me to disable Turtle Flip");
	}
	
	private void stopTurtleService()
	{
		stopService(new Intent(TurtleFlip.this, TurtleService.class));
		bubbleText.setText("Press me to enable Turtle Flip");
	}

}